/*
 * Copyright (c) 2018. czmc All Rights Reserved.
 * Author: czmc
 * Email: zxc6355533@163.com
 * Encoding: UTF-8
 */

package top.stime21.myneighbours.module.main

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.support.design.widget.BottomNavigationView
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentTransaction
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import top.stime21.framework.extension.toActivity
import top.stime21.myneighbours.Events
import top.stime21.myneighbours.R
import top.stime21.myneighbours.SplashActivity
import top.stime21.myneighbours.module.home.HomeFragment
import top.stime21.myneighbours.module.me.MeFragment
import java.util.*





class MainActivity : AppCompatActivity(){
    private val fragments: ArrayList<Fragment>  by lazy<ArrayList<Fragment> >{
        arrayListOf(HomeFragment(),/*TopicFragment(),*/ MeFragment())
    }
     var lastpos:Int = -1 ;

    private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_home -> {
                showTab(0)
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_notifications -> {
                showTab(1)
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }
    fun showTab(index:Int){
        if(index == lastpos) return
        val fragment = fragments.get(index)
        val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        if(lastpos!=-1){
            transaction.hide(fragments.get(lastpos))
        }
        if(!fragment.isAdded){
            transaction.add(R.id.frame,fragment,"${index}")
        }else{
            if(fragment.isHidden){
                transaction.show(fragment)
            }
        }
        transaction.commit()
        lastpos = index
       // Log.i("Test",   "fragments.size: ${supportFragmentManager.fragments.size},isNotnull${supportFragmentManager.findFragmentByTag("${index}")!=null}" );
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        EventBus.getDefault().register(this)
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
        showTab(0)
        if (Build.VERSION.SDK_INT >= 23) {
            val permissions = arrayOf<String>(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE)

            if (checkSelfPermission(permissions[0]) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(permissions, 0)
            }
        }

    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {

        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        //可在此继续其他操作。

    }
    @Subscribe
    fun onRecvLogoutEvent(e:Events.Logout){
        finish()
        toActivity<SplashActivity>()
    }

    override fun onDestroy() {
        super.onDestroy()
        if(EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this)
        }
    }

}
